package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.*;
//此类构建管理员对景区通告进行增删改查的界面
public class ManagerNotice extends JFrame {
	private JPanel managerNoticePanel;

	public ManagerNotice() {
		super("通知通告");
		setSize(1000, 500);
		setLocation(250, 150);

		Font font = new Font("宋体", Font.PLAIN, 25);
		UIManager.put("OptionPane.font", font);
		UIManager.put("OptionPane.messageFont", font);
		UIManager.put("OptionPane.buttonFont", font);

		setVisible(true);
	}
	
	function.Notice notice = new function.Notice();

	public void managerNoticePanel(String manager) {
		managerNoticePanel = new JPanel();
		managerNoticePanel.setLayout(null);

		ButtonGroup group0 = new ButtonGroup();
		/*将每个公告做成一个JRadioButton
		 * 组成一个ButtonGroup显示在界面左侧
		 * 管理员可通过选择不同公告而容易地对公告进行增删改查
		 */
		ArrayList<String> list = notice.display();
		for (int i = 0; i < list.size(); i++) {
			JRadioButton rb = new JRadioButton(list.get(i), true);
			rb.setBounds(50, 50 + 30 * i, 300, 30);
			rb.setFont(new Font("宋体", Font.PLAIN, 25));
			managerNoticePanel.add(rb);
			group0.add(rb);
		}

		JLabel informationLabel = new JLabel("");
		informationLabel.setBounds(300, 200, 550, 150);
		informationLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(informationLabel);

		JLabel codeLabel = new JLabel("code");
		codeLabel.setBounds(300, 50, 70, 30);
		codeLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(codeLabel);

		JLabel codeText = new JLabel("");
		codeText.setBounds(400, 50, 100, 30);
		codeText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(codeText);

		JLabel managerLabel = new JLabel("管理员");
		managerLabel.setBounds(520, 50, 120, 30);
		managerLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(managerLabel);

		JLabel managerText = new JLabel("");
		managerText.setBounds(650, 50, 200, 30);
		managerText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(managerText);

		JLabel contentLabel = new JLabel("通告");
		contentLabel.setBounds(360, 100, 100, 30);
		contentLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(contentLabel);

		JTextArea contentText = new JTextArea(1, 3);
		contentText.setBounds(420, 100, 400, 80);
		contentText.setFont(new Font("宋体", Font.PLAIN, 25));
		contentText.setLineWrap(true);
		managerNoticePanel.add(contentText);
		/*管理员编辑公告内容并点击添加按钮
		 * 即可将新的notice写入notice.xml
		 * 且新添加的notice的manager属性自动设置为当前登录的管理员
		 */
		JButton buttonSaveAdd = new JButton("添加");
		buttonSaveAdd.setBounds(80, 400, 150, 40);
		buttonSaveAdd.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(buttonSaveAdd);

		buttonSaveAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//新添加的公告的编号（code）自动设为  已有公告数+1
				codeText.setText(Integer.toString(notice.getTotalNumber() + 1));
//				新添加的notice的manager属性自动设置为当前登录的管理员
				managerText.setText(manager);
				notice = new function.Notice(codeText.getText(), managerText.getText(), contentText.getText());
//				调用notice类的add方法添加新的notice
				boolean a = notice.add();
				//若添加成功（此时a==true）则同时将此notice加入按钮组在窗口中显示出来
				if (a) {
					JRadioButton rb = new JRadioButton(codeText.getText() + "   " + managerText.getText(), true);
					ArrayList<String> list = notice.display();
					rb.setBounds(50, 50 + 30 * (list.size() - 1), 150, 30);
					rb.setFont(new Font("宋体", Font.PLAIN, 25));
					managerNoticePanel.add(rb);
					group0.add(rb);
					//添加成功后清除页面
					codeText.setText("");
					managerText.setText("");
					contentText.setText("");
//					提示添加是否成功
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "添加失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		JButton button2 = new JButton("删除");
		button2.setBounds(250, 400, 100, 40);
		button2.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(button2);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				AbstractButton btn;
				while (radioBtns.hasMoreElements()) {
					btn = radioBtns.nextElement();
//					删除选中notice
					if (btn.isSelected()) {
						enable = btn.getText();
						group0.remove(btn);
						btn.setVisible(false);
						break;
					}
				}
//				提示删除是否成功
				boolean a = notice.delete(enable);
				if (a) {
					JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
				}

			}
		});
			/*在修改时，管理员选中要修改的notice，
			 * 先点击“修改”按钮，会将被选中的notice的内容显示出来
			 然后管理员可在已有内容上进行修改，
			 修改完毕后点击“保存修改”按钮即可将修改保存到notice.xml中
			 	*/
		JButton button3 = new JButton("修改");
		button3.setBounds(380, 400, 100, 40);
		button3.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(button3);
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}
				//调用notice类的find方法返回按钮选中的notice对象
				notice = notice.find(enable);
				//将选中notice的信息打印出来
				codeText.setText(notice.getCode());
				managerText.setText(notice.getManager());
				contentText.setText(notice.getContent());
			}
		});
		
		JButton buttonSaveModify = new JButton("保存修改");
		buttonSaveModify.setBounds(500, 400, 180, 40);
		buttonSaveModify.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(buttonSaveModify);
		buttonSaveModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}

				notice = new function.Notice(codeText.getText(), managerText.getText(), contentText.getText());
//				调用notice类的modify方法实现修改
				boolean a = notice.modify(enable);
				Enumeration<AbstractButton> radioBtns1 = group0.getElements();
				if (a) {
					while (radioBtns1.hasMoreElements()) {
						AbstractButton btn = radioBtns1.nextElement();
						if (btn.isSelected()) {
							btn.setText(codeText.getText() + "   " + managerText.getText());
							break;
						}
					}
					//修改成功后清除界面
					codeText.setText("");
					managerText.setText("");
					contentText.setText("");

					JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		JButton button = new JButton("查询");
		button.setBounds(700, 400, 110, 40);
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}
				//调用notice类的inquire方法实现查询
				String information = notice.inquire(enable);
//				将查询结果打印出来
				informationLabel.setText(information);
			}
		});
		//点击离开按钮关闭当前窗口
		JButton button4 = new JButton("离开");
		button4.setBounds(840, 400, 100, 40);
		button4.setFont(new Font("宋体", Font.PLAIN, 25));
		managerNoticePanel.add(button4);
		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		managerNoticePanel.setVisible(true);
		add(managerNoticePanel);
		setVisible(true);
	}
}
